﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace WeChatParking.Core.BLL
{
    public class Fee_cfg
    {
        /// <summary>
        /// 添加费率
        /// </summary>
        /// <param name="model"></param>
        /// <param name="Enterprise_ID"></param>
        /// <returns></returns>
        private int Add(Entity.Api.Fee_cfg model, int Enterprise_ID)
        {
            int newID = -1;
            Entity.Fee_cfg cfg = new Entity.Fee_cfg();
            List<Entity.FeeType> Listfee = new List<Entity.FeeType>();
            try
            {
                cfg.Enterprise_ID = Enterprise_ID;
                cfg.CarType = model.Car_type;
                cfg.BegTime = model.BegTime.Substring(0, 2) + ":" + model.BegTime.Substring(2, 2) + ":" + model.BegTime.Substring(4, 2);
                cfg.EndTime = model.EndTime.Substring(0, 2) + ":" + model.EndTime.Substring(2, 2) + ":" + model.EndTime.Substring(4, 2);
                cfg.FeeName = model.FeeName.TrimStart(' ');

                cfg.FeeMax = Convert.ToDecimal((model.FeeMax / 100).ToString() + "." + ((model.FeeMax % 100).ToString().Length > 1 ? (model.FeeMax % 100).ToString() : "0" + (model.FeeMax % 100).ToString()));
                cfg.Guid = model.Guid;
                for (int i = 0; i < model.OrderID; i++)
                {
                    Listfee.Add(new Entity.FeeType()
                     {
                         Enterprise_ID = Enterprise_ID,
                         Name = model.FeeName,
                         OrderID = i + 1,
                         FeeValue = Convert.ToDecimal((Convert.ToInt32(model.Fee_type.Substring(i * 11 + 1, 4)) / 100).ToString() + "." + (((Convert.ToInt32(model.Fee_type.Substring(i * 11 + 1, 4)) % 100).ToString()).Length > 1 ? ((Convert.ToInt32(model.Fee_type.Substring(i * 11 + 1, 4)) % 100).ToString()) : "0" + ((Convert.ToInt32(model.Fee_type.Substring(i * 11 + 1, 4)) % 100).ToString()))),
                         TimeLong = (Convert.ToInt32(model.Fee_type.Substring(i * 11 + 5, 2)) / 100),
                         Guid = model.Guid
                     });
                }
                using (var dbContext = Repository.DbContext.Create())
                {
                    newID = dbContext.Insert(cfg);
                    foreach (var item in Listfee)
                    {
                        dbContext.Insert(item);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex, "Server同步费率失败");
            }
            return newID;
        }
        private bool Update(Entity.Api.Fee_cfg model, int Enterprise_ID)
        {
            bool isOK = false;
            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { FeeName = model.FeeName.TrimStart(' '), Enterprise_ID = Enterprise_ID };
                var cfg = dbContext.GetList<Entity.Fee_cfg>(precidate, null).FirstOrDefault();
                cfg.CarType = model.Car_type;
                cfg.BegTime = model.BegTime.Substring(0, 2) + ":" + model.BegTime.Substring(2, 2) + ":" + model.BegTime.Substring(4, 2);
                cfg.EndTime = model.EndTime.Substring(0, 2) + ":" + model.EndTime.Substring(2, 2) + ":" + model.EndTime.Substring(4, 2);
                cfg.FeeMax = Convert.ToDecimal((model.FeeMax / 100).ToString() + "." + ((model.FeeMax % 100).ToString().Length > 1 ? (model.FeeMax % 100).ToString() : "0" + (model.FeeMax % 100).ToString()));
                cfg.Guid = model.Guid;

                var precidate2 = new { Name = model.FeeName.TrimStart(' '), Enterprise_ID = Enterprise_ID };
                var ListfeeUp = dbContext.GetList<Entity.FeeType>(precidate2, null).ToList();
                List<Entity.FeeType> Listfee = new List<Entity.FeeType>();
                for (int i = 0; i < model.OrderID; i++)
                {

                    Entity.FeeType feetype = new Entity.FeeType()
                        {
                            Enterprise_ID = Enterprise_ID,
                            Name = model.FeeName.TrimStart(' '),
                            OrderID = i + 1,
                            FeeValue = Convert.ToDecimal((Convert.ToInt32(model.Fee_type.Substring(i * 11 + 1, 4)) / 100).ToString() + "." + (((Convert.ToInt32(model.Fee_type.Substring(i * 11 + 1, 4)) % 100).ToString()).Length > 1 ? ((Convert.ToInt32(model.Fee_type.Substring(i * 11 + 1, 4)) % 100).ToString()) : "0" + ((Convert.ToInt32(model.Fee_type.Substring(i * 11 + 1, 4)) % 100).ToString()))),
                            TimeLong = (Convert.ToInt32(model.Fee_type.Substring(i * 11 + 5, 2)) / 100),
                            Guid = model.Guid
                        };
                    var fee = ListfeeUp.Where(t => t.OrderID == i + 1).FirstOrDefault();
                    if (fee != null)
                        feetype.id = fee.id;
                    else
                        feetype.id = 0;
                    Listfee.Add(feetype);
                }
                isOK = dbContext.Update<Entity.Fee_cfg>(cfg);
                foreach (var item in Listfee)
                {
                    if (item.id > 0)
                    {
                        isOK = dbContext.Update<Entity.FeeType>(item);
                    }
                    else
                    {
                        dbContext.Insert<Entity.FeeType>(item);
                    }
                }
            }
            return isOK;
        }
        public bool AddORUpdate(Entity.Api.Fee_cfg model)
        {
            bool isOK = false;
            Core.BLL.Device debll = new Device();
            var device = debll.Get(model.Parking_code);
            if (this.ExistsName(model.FeeName.TrimStart(' ')))
            {
                isOK = this.Update(model, device.Enterprise_ID);
            }
            else
            {
                isOK = this.Add(model, device.Enterprise_ID) > 0;
            }
            string json = WeChatParking.Util.Conv.ToJson(model);
            LogHelper.Info(json, Entity.Enums.LogType.Server_PayFeeType);
            return isOK;
        }
        /// <summary>
        /// 是否存在同名
        /// </summary>
        /// <param name="UserCode"></param>
        /// <returns></returns>
        public bool ExistsName(string Name)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { FeeName = Name };
                return dbContext.Count<Entity.Fee_cfg>(precidate, null) > 0;
            }
        }
        /// <summary>
        /// 费用计算
        /// </summary>
        /// <param name="carModel"></param>
        /// <returns></returns>
        public decimal CountPayFee(Entity.CarRecord carModel)
        {
            return this.NewCostFree(carModel);
        }
        /// <summary>
        /// 获取费率
        /// </summary>
        /// <param name="Enterprise_ID"></param>
        /// <param name="CarUserTypeID"></param>
        /// <returns></returns>
        private List<Entity.Fee_cfg> GetFee_cfg(int Enterprise_ID, int CarUserTypeID)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { Enterprise_ID = Enterprise_ID, CarType = CarUserTypeID };
                return dbContext.GetList<Entity.Fee_cfg>(precidate, null).ToList();
            }
        }
        private DataTable GetFeeType(string Name)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { Name = Name };
                var list = dbContext.GetList<Entity.FeeType>(precidate, null).ToList();
                if (list != null && list.Count > 0)
                {
                    return this.ToDataTable(list);
                }
                else
                {
                    return null;
                }
            }
        }
        #region List to DataTable
        /// <summary>
        /// Convert a List{T} to a DataTable.
        /// </summary>
        private DataTable ToDataTable<T>(List<T> items)
        {
            var tb = new DataTable(typeof(T).Name);

            System.Reflection.PropertyInfo[] props = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

            foreach (System.Reflection.PropertyInfo prop in props)
            {
                Type t = this.GetCoreType(prop.PropertyType);
                tb.Columns.Add(prop.Name, t);
            }

            foreach (T item in items)
            {
                var values = new object[props.Length];

                for (int i = 0; i < props.Length; i++)
                {
                    values[i] = props[i].GetValue(item, null);
                }

                tb.Rows.Add(values);
            }

            return tb;
        }
        /// <summary>
        /// Determine of specified type is nullable
        /// </summary>
        public bool IsNullable(Type t)
        {
            return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
        }
        /// <summary>
        /// Return underlying type if type is Nullable otherwise return the type
        /// </summary>
        public Type GetCoreType(Type t)
        {
            if (t != null && this.IsNullable(t))
            {
                if (!t.IsValueType)
                {
                    return t;
                }
                else
                {
                    return Nullable.GetUnderlyingType(t);
                }
            }
            else
            {
                return t;
            }
        }
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="InTime">进时间</param>
        /// <param name="OutTime">出时间</param>
        /// <param name="CarType">车型</param>
        /// <returns>费用</returns>
        private decimal NewCostFree(Entity.CarRecord carModel)
        {
            List<Entity.Fee_cfg> fee_cfg = this.GetFee_cfg(carModel.Enterprise_ID, Convert.ToInt32(carModel.retain1));
            if (fee_cfg != null && fee_cfg.Count > 0)
            {
                Decimal MaxFee = fee_cfg.FirstOrDefault().FeeMax;
                DataTable cfgdt = this.ToDataTable(fee_cfg);
                decimal Fee = this.GetFee(DateTime.Now, carModel.inTime, cfgdt, MaxFee);
                return Fee;
            }
            else
            {
                return 0.00m;
            }

        }
        /// <summary>
        /// 费用计算
        /// </summary>
        /// <param name="OutTime"></param>
        /// <param name="InTime"></param>
        /// <param name="cfgdt"></param>
        /// <param name="MaxFee"></param>
        /// <returns></returns>
        private decimal GetFee(DateTime OutTime, DateTime InTime, DataTable cfgdt, Decimal MaxFee)
        {
            decimal Fee = 0.00M;
            #region 分时段
            if (cfgdt.Rows.Count > 1)
            {
                //时间相减
                TimeSpan ts = OutTime - InTime;
                //停车天数
                int d = ts.Days;
                //小时数
                int h = ts.Hours;
                //分钟数
                int m = ts.Minutes;

                int hXm = h * 60 + m;
                Console.WriteLine(ts);
                string FeeName = cfgdt.Rows[0]["FeeName"].ToString();
                for (int i = 0; i < cfgdt.Rows.Count; i++)
                {
                    DataTable typeDt = this.GetFeeType(cfgdt.Rows[i]["FeeName"].ToString());
                    //上限时间
                    DateTime begTime = DateTime.Parse(cfgdt.Rows[i]["BegTime"].ToString());
                    //下限时间
                    DateTime endTime = DateTime.Parse(cfgdt.Rows[i]["EndTime"].ToString());


                    int[] TimeLong = new int[typeDt.Rows.Count];
                    decimal[] FeeValue = new decimal[typeDt.Rows.Count];
                    for (int j = 0; j < typeDt.Rows.Count; j++)
                    {
                        TimeLong[j] = int.Parse(typeDt.Rows[j]["TimeLong"].ToString());
                        FeeValue[j] = decimal.Parse(typeDt.Rows[j]["FeeValue"].ToString());
                    }

                    if (d > 0)
                    {
                        if (InTime.Hour >= begTime.Hour && InTime.Month >= begTime.Month)
                        {

                            #region 在区间内
                            if (OutTime.Hour <= endTime.Hour && OutTime.Month <= endTime.Month)
                            {
                                int TimeM = (OutTime - InTime).Hours * 60 + (OutTime - InTime).Minutes;
                                for (int k = 0; k < TimeLong.Length; k++)
                                {
                                    if (TimeM > 0)
                                    {
                                        Fee += FeeValue[k];
                                        TimeM -= TimeLong[k];
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                if (Fee > 0)
                                {
                                    Fee += (TimeM / TimeLong[TimeLong.Length - 1]) * FeeValue[FeeValue.Length - 1];
                                    if (hXm % TimeLong[TimeLong.Length - 1] > 0)
                                    {
                                        Fee += FeeValue[FeeValue.Length - 1];
                                    }
                                }
                            #endregion
                                if (Fee > MaxFee)
                                {
                                    Fee = MaxFee;
                                }
                                //天数X日封顶
                                Fee += (MaxFee * d);
                                return Fee;
                            }


                            //有部分在区间外
                            else
                            {
                                #region 未超出部分
                                int TimeM = (endTime - InTime).Hours * 60 + (endTime - InTime).Minutes;
                                for (int k = 0; k < TimeLong.Length; k++)
                                {
                                    if (TimeM > 0)
                                    {
                                        Fee += FeeValue[k];
                                        TimeM -= TimeLong[k];
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                if (Fee > 0)
                                {
                                    Fee += (TimeM / TimeLong[TimeLong.Length - 1]) * FeeValue[FeeValue.Length - 1];
                                    if (hXm % TimeLong[TimeLong.Length - 1] > 0)
                                    {
                                        Fee += FeeValue[FeeValue.Length - 1];
                                    }
                                }
                                #endregion
                                #region 出场时间超出部分
                                int TimeMMax = (OutTime - InTime).Hours * 60 + (OutTime - InTime).Minutes;
                                TimeM = TimeMMax - TimeM;



                                DataTable typeDt2 = this.GetFeeType(cfgdt.Rows[1]["FeeName"].ToString());
                                int[] TimeLong2 = new int[typeDt2.Rows.Count];
                                decimal[] FeeValue2 = new decimal[typeDt2.Rows.Count];
                                for (int j = 0; j < typeDt2.Rows.Count; j++)
                                {
                                    TimeLong2[j] = int.Parse(typeDt2.Rows[j]["TimeLong"].ToString());
                                    FeeValue2[j] = decimal.Parse(typeDt2.Rows[j]["FeeValue"].ToString());
                                }
                                for (int k = 0; k < TimeLong2.Length; k++)
                                {
                                    if (TimeM > 0)
                                    {
                                        Fee += FeeValue2[k];
                                        TimeM -= TimeLong2[k];
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                if (Fee > 0)
                                {
                                    Fee += (TimeM / TimeLong2[TimeLong2.Length - 1]) * FeeValue[FeeValue2.Length - 1];
                                    if (hXm % TimeLong[TimeLong2.Length - 1] > 0)
                                    {
                                        Fee += FeeValue2[FeeValue2.Length - 1];
                                    }
                                }
                                #endregion

                                if (Fee > MaxFee)
                                {
                                    Fee = MaxFee;
                                }
                                //天数X日封顶
                                Fee += (MaxFee * d);
                                return Fee;
                            }

                        }
                        if (OutTime.Hour <= endTime.Hour && OutTime.Month <= endTime.Month)
                        {

                            #region 在区间内
                            if (InTime.Hour >= begTime.Hour && InTime.Month >= begTime.Month)
                            {
                                int TimeM = (OutTime - InTime).Hours * 60 + (OutTime - InTime).Minutes;
                                for (int k = 0; k < TimeLong.Length; k++)
                                {
                                    if (TimeM > 0)
                                    {
                                        Fee += FeeValue[k];
                                        TimeM -= TimeLong[k];
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                if (Fee > 0)
                                {
                                    Fee += (TimeM / TimeLong[TimeLong.Length - 1]) * FeeValue[FeeValue.Length - 1];
                                    if (hXm % TimeLong[TimeLong.Length - 1] > 0)
                                    {
                                        Fee += FeeValue[FeeValue.Length - 1];
                                    }
                                }
                            #endregion
                                if (Fee > MaxFee)
                                {
                                    Fee = MaxFee;
                                }
                                //天数X日封顶
                                Fee += (MaxFee * d);
                                return Fee;
                            }
                            //有部分在区间外
                            else
                            {
                                #region 未超出部分
                                int TimeM = (OutTime - begTime).Hours * 60 + (OutTime - begTime).Minutes;
                                for (int k = 0; k < TimeLong.Length; k++)
                                {
                                    if (TimeM > 0)
                                    {
                                        Fee += FeeValue[k];
                                        TimeM -= TimeLong[k];
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                if (Fee > 0)
                                {
                                    Fee += (TimeM / TimeLong[TimeLong.Length - 1]) * FeeValue[FeeValue.Length - 1];
                                    if (hXm % TimeLong[TimeLong.Length - 1] > 0)
                                    {
                                        Fee += FeeValue[FeeValue.Length - 1];
                                    }
                                }
                                #endregion
                                #region 进场时间超出部分

                                int TimeMMax = (OutTime - InTime).Hours * 60 + (OutTime - InTime).Minutes;
                                TimeM = TimeMMax - TimeM;



                                DataTable typeDt2 = this.GetFeeType(cfgdt.Rows[1]["FeeName"].ToString());
                                int[] TimeLong2 = new int[typeDt2.Rows.Count];
                                decimal[] FeeValue2 = new decimal[typeDt2.Rows.Count];
                                for (int j = 0; j < typeDt2.Rows.Count; j++)
                                {
                                    TimeLong2[j] = int.Parse(typeDt2.Rows[j]["TimeLong"].ToString());
                                    FeeValue2[j] = decimal.Parse(typeDt2.Rows[j]["FeeValue"].ToString());
                                }
                                for (int k = 0; k < TimeLong2.Length; k++)
                                {
                                    if (TimeM > 0)
                                    {
                                        Fee += FeeValue2[k];
                                        TimeM -= TimeLong2[k];
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                if (Fee > 0)
                                {
                                    Fee += (TimeM / TimeLong2[TimeLong2.Length - 1]) * FeeValue[FeeValue2.Length - 1];
                                    if (hXm % TimeLong[TimeLong2.Length - 1] > 0)
                                    {
                                        Fee += FeeValue2[FeeValue2.Length - 1];
                                    }
                                }
                                #endregion

                                if (Fee > MaxFee)
                                {
                                    Fee = MaxFee;
                                }
                                //天数X日封顶
                                Fee += (MaxFee * d);
                                return Fee;
                            }
                        }

                    }
                    else
                    {

                        if (InTime.Hour >= begTime.Hour && InTime.Month >= begTime.Month)
                        {

                            #region 在区间内
                            if (OutTime.Hour <= endTime.Hour && OutTime.Month <= endTime.Month)
                            {
                                int TimeM = (OutTime - InTime).Hours * 60 + (OutTime - InTime).Minutes;
                                for (int k = 0; k < TimeLong.Length; k++)
                                {
                                    if (TimeM > 0)
                                    {
                                        Fee += FeeValue[k];
                                        TimeM -= TimeLong[k];
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                if (Fee > 0)
                                {
                                    Fee += (TimeM / TimeLong[TimeLong.Length - 1]) * FeeValue[FeeValue.Length - 1];
                                    if (hXm % TimeLong[TimeLong.Length - 1] > 0)
                                    {
                                        Fee += FeeValue[FeeValue.Length - 1];
                                    }
                                }
                            #endregion
                                if (Fee > MaxFee)
                                {
                                    Fee = MaxFee;
                                }
                                return Fee;
                            }


                            //有部分在区间外
                            else
                            {
                                #region 未超出部分
                                int TimeM = (endTime - InTime).Hours * 60 + (endTime - InTime).Minutes;
                                for (int k = 0; k < TimeLong.Length; k++)
                                {
                                    if (TimeM > 0)
                                    {
                                        Fee += FeeValue[k];
                                        TimeM -= TimeLong[k];
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                if (Fee > 0)
                                {
                                    Fee += (TimeM / TimeLong[TimeLong.Length - 1]) * FeeValue[FeeValue.Length - 1];
                                    if (hXm % TimeLong[TimeLong.Length - 1] > 0)
                                    {
                                        Fee += FeeValue[FeeValue.Length - 1];
                                    }
                                }
                                #endregion
                                #region 出场时间超出部分
                                int TimeMMax = (OutTime - InTime).Hours * 60 + (OutTime - InTime).Minutes;
                                TimeM = TimeMMax - TimeM;



                                DataTable typeDt2 = this.GetFeeType(cfgdt.Rows[1]["FeeName"].ToString());
                                int[] TimeLong2 = new int[typeDt2.Rows.Count];
                                decimal[] FeeValue2 = new decimal[typeDt2.Rows.Count];
                                for (int j = 0; j < typeDt2.Rows.Count; j++)
                                {
                                    TimeLong2[j] = int.Parse(typeDt2.Rows[j]["TimeLong"].ToString());
                                    FeeValue2[j] = decimal.Parse(typeDt2.Rows[j]["FeeValue"].ToString());
                                }
                                for (int k = 0; k < TimeLong2.Length; k++)
                                {
                                    if (TimeM > 0)
                                    {
                                        Fee += FeeValue2[k];
                                        TimeM -= TimeLong2[k];
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                if (Fee > 0)
                                {
                                    Fee += (TimeM / TimeLong2[TimeLong2.Length - 1]) * FeeValue[FeeValue2.Length - 1];
                                    if (hXm % TimeLong[TimeLong2.Length - 1] > 0)
                                    {
                                        Fee += FeeValue2[FeeValue2.Length - 1];
                                    }
                                }
                                #endregion

                                if (Fee > MaxFee)
                                {
                                    Fee = MaxFee;
                                }
                                return Fee;
                            }

                        }
                        if (OutTime.Hour <= endTime.Hour && OutTime.Month <= endTime.Month)
                        {

                            #region 在区间内
                            if (InTime.Hour >= begTime.Hour && InTime.Month >= begTime.Month)
                            {
                                int TimeM = (OutTime - InTime).Hours * 60 + (OutTime - InTime).Minutes;
                                for (int k = 0; k < TimeLong.Length; k++)
                                {
                                    if (TimeM > 0)
                                    {
                                        Fee += FeeValue[k];
                                        TimeM -= TimeLong[k];
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                if (Fee > 0)
                                {
                                    Fee += (TimeM / TimeLong[TimeLong.Length - 1]) * FeeValue[FeeValue.Length - 1];
                                    if (hXm % TimeLong[TimeLong.Length - 1] > 0)
                                    {
                                        Fee += FeeValue[FeeValue.Length - 1];
                                    }
                                }
                            #endregion
                                if (Fee > MaxFee)
                                {
                                    Fee = MaxFee;
                                }
                                return Fee;
                            }



                            //有部分在区间外
                            else
                            {
                                #region 未超出部分
                                int TimeM = (OutTime - begTime).Hours * 60 + (OutTime - begTime).Minutes;
                                for (int k = 0; k < TimeLong.Length; k++)
                                {
                                    if (TimeM > 0)
                                    {
                                        Fee += FeeValue[k];
                                        TimeM -= TimeLong[k];
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                if (Fee > 0)
                                {
                                    Fee += (TimeM / TimeLong[TimeLong.Length - 1]) * FeeValue[FeeValue.Length - 1];
                                    if (hXm % TimeLong[TimeLong.Length - 1] > 0)
                                    {
                                        Fee += FeeValue[FeeValue.Length - 1];
                                    }
                                }
                                #endregion
                                #region 进场时间超出部分

                                int TimeMMax = (OutTime - InTime).Hours * 60 + (OutTime - InTime).Minutes;
                                TimeM = TimeMMax - TimeM;



                                DataTable typeDt2 = this.GetFeeType(cfgdt.Rows[1]["FeeName"].ToString());
                                int[] TimeLong2 = new int[typeDt2.Rows.Count];
                                decimal[] FeeValue2 = new decimal[typeDt2.Rows.Count];
                                for (int j = 0; j < typeDt2.Rows.Count; j++)
                                {
                                    TimeLong2[j] = int.Parse(typeDt2.Rows[j]["TimeLong"].ToString());
                                    FeeValue2[j] = decimal.Parse(typeDt2.Rows[j]["FeeValue"].ToString());
                                }
                                for (int k = 0; k < TimeLong2.Length; k++)
                                {
                                    if (TimeM > 0)
                                    {
                                        Fee += FeeValue2[k];
                                        TimeM -= TimeLong2[k];
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                if (Fee > 0)
                                {
                                    Fee += (TimeM / TimeLong2[TimeLong2.Length - 1]) * FeeValue[FeeValue2.Length - 1];
                                    if (hXm % TimeLong[TimeLong2.Length - 1] > 0)
                                    {
                                        Fee += FeeValue2[FeeValue2.Length - 1];
                                    }
                                }
                                #endregion

                                if (Fee > MaxFee)
                                {
                                    Fee = MaxFee;
                                }
                                return Fee;
                            }
                        }

                    }
                }
            }
            #endregion
            #region 按普通计费模式
            else
            {
                string FeeName = cfgdt.Rows[0]["FeeName"].ToString();
                DataTable typeDb = this.GetFeeType(FeeName);
                int[] TimeLong = new int[typeDb.Rows.Count];
                decimal[] FeeValue = new decimal[typeDb.Rows.Count];

                for (int i = 0; i < typeDb.Rows.Count; i++)
                {
                    TimeLong[i] = int.Parse(typeDb.Rows[i]["TimeLong"].ToString());
                    FeeValue[i] = decimal.Parse(typeDb.Rows[i]["FeeValue"].ToString());
                }
                //时间相减
                TimeSpan ts = OutTime - InTime;
                Console.WriteLine(ts);
                //停车天数
                int d = ts.Days;
                //小时数
                int h = ts.Hours;
                //分钟数
                int m = ts.Minutes;

                int hXm = h * 60 + m;

                //天数X日封顶
                if (d > 0)
                {

                    for (int i = 0; i < TimeLong.Length; i++)
                    {
                        if (hXm > 0)
                        {
                            Fee += FeeValue[i];
                            hXm -= TimeLong[i];
                        }
                        else
                        {
                            break;
                        }
                    }
                    Fee += (hXm / TimeLong[TimeLong.Length - 1]) * FeeValue[FeeValue.Length - 1];
                    if (hXm % TimeLong[TimeLong.Length - 1] > 0)
                    {
                        Fee += FeeValue[FeeValue.Length - 1];
                    }
                    if (Fee > MaxFee)
                    {
                        Fee = MaxFee;
                    }
                    //天数X日封顶
                    Fee += (MaxFee * d);
                }
                else
                {

                    for (int i = 0; i < TimeLong.Length; i++)
                    {
                        if (hXm > 0)
                        {
                            Fee += FeeValue[i];
                            hXm -= TimeLong[i];
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (hXm > 0)
                    {
                        Fee += (hXm / TimeLong[TimeLong.Length - 1]) * FeeValue[FeeValue.Length - 1];
                        if (hXm % TimeLong[TimeLong.Length - 1] > 0)
                        {
                            Fee += FeeValue[FeeValue.Length - 1];
                        }
                    }
                    if (Fee > MaxFee)
                    {
                        Fee = MaxFee;
                    }
                }
            }
            return Fee;
            #endregion
        }

    }
}
